<?php

/* 系统通知 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class SysnoticeController extends AdminBaseController
{

    public function index()
    {
        $data = $this->request->param();
        $map=[];

        $start_time=isset($data['start_time']) ? $data['start_time']: '';
        $end_time=isset($data['end_time']) ? $data['end_time']: '';
        if($start_time!=""){
           $map[]=['addtime','>=',strtotime($start_time)];
        }
        if($end_time!=""){
           $map[]=['addtime','<=',strtotime($end_time) + 60*60*24];
        }
		
		$keyword=isset($data['keyword']) ? $data['keyword']: '';
        if($keyword!=''){
            $map[]=['content','like',"%".$keyword."%"];
        }

        if (empty($data['langId'])) {
            $data['langId'] = $this->defaultLanguage['id'];
            $this->assign('langId', $this->defaultLanguage['id']);
        }else {
            $this->assign('langId', $data['langId']);
        }
        $map[] = ['lang_id', '=', $data['langId']];

  
   
        $list = Db::name('sys_notice')
            ->where("touid=0")
            ->where($map)
            ->order("id desc")
            ->paginate(20);
        
        $list->appends($data);
        
        $page = $list->render();
        $this->assign("page", $page);
            
        $this->assign('list', $list);
        $this->assign('countryData', $this->recommendLanguageData);
        return $this->fetch();
    }


    public function add()
    {
        $this->assign('countryData', $this->recommendLanguageData);
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            

            $content=$data['content'];
            
            if($content==''){
                $this->error('请输入消息内容');
            }

            $ip=get_client_ip(0, true);
            
            $ip=ip2long($ip);
            
            $admin_id = session('ADMIN_ID');
            $admininfo = Db::name('user')->where("id='{$admin_id}'")->find();
            
            $admin=$admininfo['user_login'];
            if($admininfo['user_nickname']){
                $admin=$admininfo['user_nickname'];
            }

            $insert=[
                'touid'=>0,
                'content'=>$content,
                'addtime'=>time(),
                'admin'=>$admin,
                'lang_id' => $data['lang_id'],
                'ip'=>$ip,
            ];
            
            $id = DB::name('sys_notice')->insertGetId($insert);
            if(!$id){
                $this->error("操作失败！");
            }
            
            /* IM */
            $ext=[
                'method'=>'sysnotice',
                'action'=>'0',
                'content'=>$content,
                'content_en'=>'',
            ];
            
            $configpri=getConfigPri();
            $group_id=$configpri['im_full_group_id'];
            $text_content = json_encode($ext,JSON_UNESCAPED_UNICODE);
            $api=getTxRestApi();


            $ret = $api->group_send_group_system_notification($group_id, $text_content,'1');
            
            //file_put_contents(CMF_ROOT.'log/sendGroupMsg.txt',date('y-m-d H:i:s').'提交参数信息 text_content:'.json_encode($text_content)."\r\n",FILE_APPEND);            
            //file_put_contents(CMF_ROOT.'log/sendGroupMsg.txt',date('y-m-d H:i:s').'提交参数信息 ret:'.json_encode($ret)."\r\n",FILE_APPEND);

            $this->success("操作成功！");
        }
    }
    
    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = DB::name('sys_notice')->where("id={$id}")->delete();
        if($rs!==false){
            $this->error("删除失败！");
        }
        $this->success("删除成功！",url("Sysnotice/index"));
    }

}